#!/usr/bin/python

import os, sys, glob, shutil, getopt, re

def main():				
	csv_files = glob.glob("*.csv")
	for f in csv_files:
		print "Writing KML file for %s" % (f,)
		csv_to_kml(f)	
	print "Done."

def csv_to_kml(input_filename):

	# open input file
	csv_file = open(input_filename,'r')
	
	# preamble 
	input_filename_base, input_filename_ext = os.path.splitext(input_filename)
		
	# open output file
	kml_file = open(input_filename_base + '.kml','w')

	kml_file.write(r"""<?xml version="1.0" encoding="utf-8" ?>
	<kml xmlns="http://www.opengis.net/kml/2.2">
	""")
	kml_file.write("<Document><name>%s</name>" % input_filename_base)
	kml_file.write(r"""
	<Schema name="sample" id="sample">
		<SimpleField name="Name" type="string"></SimpleField>
		<SimpleField name="Description" type="string"></SimpleField>
		<SimpleField name="L2" type="int"></SimpleField>
		<SimpleField name="L1" type="int"></SimpleField>
		<SimpleField name="L0" type="int"></SimpleField>
		<SimpleField name="ID_unit" type="int"></SimpleField>
		<SimpleField name="Prob_.3._stage" type="float"></SimpleField>
		<SimpleField name="Prob" type="float"></SimpleField>
	</Schema>
	""")

	# main loop 
	first_line_flag = 1
	for line in csv_file:
		fields = map(lambda s: re.sub(r'^"|"$|^\'|\'$', '', s.strip()), line.split(","))
		if (first_line_flag == 1):
			headers = fields
			n = len(headers)
			first_line_flag = 0
			print headers
		else:
			kml_file.write('  <Placemark>\n')
	#		kml_file.write('  <name>L2=%s, L1=%s, L0=%s</name>\n' % (fields[3],fields[4],fields[5]))
			kml_file.write('\t<ExtendedData><SchemaData schemaUrl=\"#sample\">\n')
			for i in xrange(3,n):
				kml_file.write('\t\t<SimpleData name=\"' + headers[i] + '\">' + fields[i] + '</SimpleData>\n')
			kml_file.write('\t\t</SchemaData></ExtendedData>\n')
			kml_file.write("     <Point><coordinates>%s,%s</coordinates></Point>\n" % (fields[1],fields[2]))
			kml_file.write('  </Placemark>\n')
	
	# epilogue
	kml_file.write('\t</Document>\n\t</kml>')
	csv_file.close()
	kml_file.close()

if __name__ == '__main__':
	main()